$(function () {
    var collections_datagrid = $("#collections_datagrid");
    var collections_dialog = $("#collections_dialog");
    var collections_add_form = $("#collections_add_form");

    collections_datagrid.datagrid({
        url: "/collections/query",
        fit: true,
        fitColumns: true,
        fit: true,
        rownumbers: true,
        columns: [[
            {field: "name", title: "学生姓名", width: 100},
            {field: "classgrade", title: "所在班级", width: 130, formatter: formatterClassGrade},
            {field: "collectionMoneyTime", title: "收款时间", width: 125},
            {field: "totalFee", title: "总费用", width: 100},
            {field: "unPaidFee", title: "未缴费用", width: 100},
            {field: "paidFee", title: "已缴费用", width: 100},
            {field: "inputEmployee", title: "营销人员", width: 100, formatter: formatterInputEmployee},
            {field: "info", title: "描述", width: 100},
            {field: "paymentType", title: "支付类型", width: 100,formatter:formatterPaymentType},
            {field: "paymentEmployee", title: "收款人员", width: 125, formatter: formatterPaymentEmployee},
            {field: "status", title: "审核状态", width: 100, formatter: formatterStatus},
            {field: "statusEmployee", title: "审核人", width: 100, formatter: formatterStatusEmployee}
        ]],
        toolbar: "#collections_toolbar",
        singleSelect: true,
        pagination: true
    });
    //编辑弹框
    collections_dialog.dialog({
        buttons: "#collections_dialog_buttons",
        closed: true
    });

    //1.把方法绑定到一个对象上
    var cmdObj = {
        //付款管理编辑
        editCollections: function () {
            //获取选中行
            var selectedRow = collections_datagrid.datagrid("getSelected");
            //判断是否选中行
            if (!selectedRow) {
                $.messager.alert("提示", "当前无人被选中", "error");
            }
            console.log(selectedRow);
            //设置表单回显
            //清空表单
            collections_add_form.form("clear");
            //数据回显
            //所在班级回显
            if (selectedRow.classgrade) {
                selectedRow['classgrade.id'] = selectedRow.classgrade.id;
            }
            //营销人员回显
            if (selectedRow.inputEmployee) {
                selectedRow['inputEmployee.id'] = selectedRow.inputEmployee.id;
            }
            //所在校区回显
            if(selectedRow.campus){
                selectedRow['campus.id'] = selectedRow.campus.id;
            }
            //所在学院回显
            if(selectedRow.college){
                selectedRow['college.id'] = selectedRow.college.id;
            }
            //付款方式回显
            if(selectedRow.payment){
                selectedRow['payment.id'] = selectedRow.payment.id;
            }
            //支付类型回显
            if(selectedRow.paymentType){
                selectedRow['paymentType.id'] = selectedRow.paymentType.id;
            }
            collections_add_form.form("load", selectedRow);
            //弹出编辑框
            collections_dialog.dialog("setTitle", "编辑");
            collections_dialog.dialog("open");

        },
        //付款管理加载
        reloadCollections: function () {
            collections_datagrid.datagrid("reload");
        },
        //提交保存
        save: function () {
            //提交表单
            collections_add_form.form("submit", {
                url: "/collections/update",
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("提示", "操作成功", "info", function () {
                            cmdObj.cancel();
                            cmdObj.reloadCollections();
                        });
                    } else {
                        $.messager.alert("提示", data.msg, "error");
                    }
                }
            });
        },
        //取消
        cancel: function () {
            //关闭添加弹窗
            collections_dialog.dialog("close");
        },
        //高级查询
        search: function () {
            //1.获取查询input里面的值
            var className = $("#classgradeId").val();
            var beginDate = $("#beginDate").val();
            var endDate = $("#endDate").val();
            var keyWord = $("#keyWord").val();
            var status = $("#status").val();

            //2.调用datagrid重新加载数据,并且带上查询条件
            collections_datagrid.datagrid("load", {
                classgradeId : className,
                beginDate : beginDate,
                endDate : endDate,
                keyWord : keyWord,
                status : status
            });
        },
        //查看
        seeCollections: function () {
            //获取选中行
            var selectedRow = collections_datagrid.datagrid("getSelected");
            //判断是否选中行
            if (!selectedRow) {
                $.messager.alert("提示", "当前无人被选中", "error");
                return;
            }
            collections_add_form.form('clear');
            collections_dialog.dialog('setTitle', '查看付款管理资料');
            collections_add_form.form('load', selectedRow);
            $(".easyui-combobox").combobox('readonly', true);
            $(".easyui-datebox").datebox('readonly', true);
            $(".easyui-textbox").textbox('readonly', true);
            $("#commit_button").linkbutton('disable');
            collections_dialog.dialog('open');
        },
        //审核
        auditCollections: function () {
            //获取选中行
            var selectedRow = collections_datagrid.datagrid("getSelected");
            //判断是否选中行
            if (!selectedRow) {
                $.messager.alert("提示", "当前无人被选中", "error");
                return;
            }
            if (selectedRow.status == 0) {
                $.messager.confirm("提示", "确定要审核当前学员吗?", function (yes) {
                    if (yes) {
                        if (selectedRow.unPaidFee != 0) {
                            $.messager.confirm("提示", "当前学员[" + selectedRow.name + "]费用未缴清,您确定要审核吗?", function (yes) {
                                if(yes) {
                                    $.post("/collections/auditCollections", {id: selectedRow.id}, function (data) {
                                        if (data.success) {
                                            $.messager.alert("提示", "审核成功", "info", function () {
                                                cmdObj.reloadCollections();
                                            });
                                        } else {
                                            $.messager.alert("提示", data.msg, "error");
                                        }
                                    });
                                }
                            });
                        }
                    }
                });
            } else {
                $.messager.alert("提示", "禁止重复审核", "error");
            }
        },
        //反审核
        unauditCollections: function () {
            //获取选中行
            var selectedRow = collections_datagrid.datagrid("getSelected");
            //判断是否选中行
            if (!selectedRow) {
                $.messager.alert("提示", "当前无人被选中", "error");
                return;
            }

            if (selectedRow.status == 1) {
                $.messager.confirm("提示", "确定要反审核当前学员吗?", function (yes) {
                    if (yes) {
                        if (selectedRow.unPaidFee != 0) {
                            $.messager.confirm("提示", "当前学员[" + selectedRow.name + "]费用未缴清,您确定要反审核吗?", function (yes) {
                                $.post("/collections/unauditCollections", {id: selectedRow.id}, function (data) {
                                    if (data.success) {
                                        $.messager.alert("提示", "反审核成功", "info", function () {
                                            cmdObj.reloadCollections();
                                        });
                                    } else {
                                        $.messager.alert("提示", data.msg, "error");
                                    }
                                });
                            });
                        }
                    }
                });
            } else {
                $.messager.alert("提示", "当前学员还未审核,请审核后再试", "error");
            }
        }
    }


    //2.按钮的点击事件通过js来绑定
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    });


});

//格式化营销人员
function formatterInputEmployee(value, row, index) {
    return value == null ? "" : value.realName;
}

//格式化收款人员
function formatterPaymentEmployee(value, row, index) {
    return value == null ? "" : value.realName;
}

//格式化审核人员
function formatterStatusEmployee(value, row, index) {
    return value == null ? "" : value.realName;
}

//格式化所在班级
function formatterClassGrade(value, row, index) {
    return value == null ? "" : value.name;
}

//格式化审核状态
function formatterStatus(value, row, index) {
    if (value == 0) {
        return "<span style='color: #d9534f'>未审核</span>";
    } else {
        return "已审核";
    }
}

//格式化支付类型
function formatterPaymentType(value, row, index) {
    return value == null ? "" : value.name;
}

function count() {
    var totalFee = $(this).val();
    var paidFee = $("#paidFee").val();
    var surplus = totalFee - paidFee;

    surplus = surplus < 0 ? 0 : surplus;

    if (surplus) {
        $("#feeStatus_combobox").combobox('select', 0);
    } else {
        $("#feeStatus_combobox").combobox('select', 1);
    }
    var unPaidFee = $("#unPaidFee");

    unPaidFee.textbox('setText', surplus);
}

function count1() {
    var paidFee = $(this).val();
    var totalFee = $("#totalFee").val();
    var surplus = totalFee - paidFee;

    surplus = surplus < 0 ? 0 : surplus;

    if (surplus) {
        $("#feeStatus_combobox").combobox('select', 0);
    } else {
        $("#feeStatus_combobox").combobox('select', 1);
    }
    var unPaidFee = $("#unPaidFee");
    unPaidFee.textbox('setText', surplus);
    unPaidFee.textbox('setValue', surplus);
}